<template>
  <div style="position:relative;">
    <loading></loading>
    <error></error>
    <div class="g-legend-wrapper">
      <span class="g-legend-title g-font-f5">
        {{currentTime}}
      </span>
      <div class="g-legend-content">
        <span class="g-font-f5 g-legend-item red">
          {{legendText.redText}}
          <span class="number">{{positive}}只</span>
        </span>
        <span class="g-font-f5 g-legend-item green">
          {{legendText.greenText}}
          <span class="number"> {{negative}}只</span>
        </span>
        <span class="g-font-f5 g-legend-item blue">
          {{legendText.blueText}}
          <span class="number">{{index}}</span>
        </span>
      </div>
    </div>
    <div ref="chartLine" class="chart-line" style="{width:1.875rem;height: 2.43rem;padding: .1rem 0;}">
    </div>
  </div>
</template>

<script>
import echarts from 'echarts/lib/echarts';
// 引入柱状图
import 'echarts/lib/chart/line';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';

import { getTradeTimeArr } from '@/common/common'

const tradeDateArr = getTradeTimeArr();

const option = {
  grid: {
    top: '12%',
    bottom: '10%',
    left: '10%',
    right: '13%'
  },
  tooltip: {
    trigger: 'axis'
    // showContent: false
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: tradeDateArr,
    splitLine: {
      show: true,
      lineStyle: {
        type: 'dotted'
      }
    },
    axisTick: {
      show: false
    },      // 隐藏刻度
    axisPointer: {
      show: true,
      type: 'line',
      lineStyle: {
        color: '#212138',
        type: 'solid'
      },
      label: {
        show: false,
        backgroundColor: 'transparent',
        textStyle: {
          color: 'transparent'
        }
      }
    },
    splitNumber: 4,
    axisLabel: {
      textStyle: {
        color: '#a5a5a5'
      }
    }   // x轴 文字样式
  },
  yAxis: [{
    name: '涨跌数(只)',
    nameTextStyle: {
      color: '#555'
    },
    splitLine: {
      show: false
    },
    axisTick: {
      show: false
    },      // 隐藏刻度
    axisLine: {
      lineStyle: {
        type: 'dotted',
        color: '#bac9d8'
      }
    },
    axisLabel: {
      textStyle: {
        color: '#a5a5a5'
      }
    },    // 轴 文字样式
    // min: -100,
    // max: 100,
    splitNumber: 2
  }, {
    name: '上证指数',
    // min: 2800,
    // max: 3200,
    nameTextStyle: {
      color: '#555'
    },
    axisTick: {
      show: false
    },    // 隐藏刻度
    splitLine: {
      lineStyle: {
        type: 'dotted'
      }
    },
    axisLine: {
      lineStyle: {
        type: 'dotted',
        color: '#bac9d8'
      }
    },
    axisLabel: {
      textStyle: {
        color: '#a5a5a5'
      }
    }   // 轴 文字样式
  }],
  series: [
    {
      name: '上证指数',
      yAxisIndex: 1,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#6c9bd2'
        }
      },
      lineStyle: {
        normal: {
          width: 1,
          color: '#6c9bd2',
          type: 'dotted'
        }
      },
      smooth: true,
      symbol: 'none'
    },
    {
      type: 'line',
      smooth: true,
      symbol: 'circle',
      showSymbol: false,
      sampling: 'average',
      data: [],
      lineStyle: {
        normal: {
          color: '#eb333b'
        }
      }
    },
    {
      type: 'line',
      smooth: true,
      symbol: 'circle',
      showSymbol: false,
      sampling: 'average',
      data: [],
      lineStyle: {
        normal: {
          color: '#1aae52'
        }
      }
    }
  ]
};

export default {
  name: 'trend-chart',
  data () {
    return {
      currentTime: '--:--',
      index: 3250,
      positive: 20,
      negative: 20
    }
  },
  props: {
    chartData: {
      type: Object
    },
    legendText: {
      type: Object,
      default: function () {
        return {
          redText: '涨停',
          greenText: '跌停',
          blueText: '上证指数'
        }
      }
    }
  },
  methods: {
    chartSelected(params) {
      let index = params[0].dataIndex;
      this.currentTime = params[0].axisValue;

      Object.assign(this.$data, {
        index: this.chartData.indexData.index[index],
        positive: this.chartData.gain[index],
        negative: this.chartData.slip[index]
      });
    },
    renderChart (val) {
      this.chartLine.setOption({
        yAxis: [{}, {
          min: Math.min.apply(null, val.indexData.index),
          max: Math.max.apply(null, val.indexData.index)
        }],
        series: [{
          data: val.indexData.index
        }, {
          data: val.gain
        }, {
          data: val.slip
        }],
        tooltip: {
          formatter: this.chartSelected
        }
      })

      setTimeout(() => {
        this.$store.dispatch('hideLoading')                // 图表渲染成功    loading消失
      }, 0)
    }
  },
  watch: {
    chartData: function (val, oldVal) {
      this.renderChart(val);
    }
  },
  mounted () {
    this.$store.dispatch('showLoading');
    this.chartLine = echarts.init(this.$refs.chartLine);
    this.chartLine.setOption(option);
  },
  beforeDestroy () {
    echarts.dispose(this.chartLine);
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
  @import '../../../assets/css/common.less';

  .g-legend-content {
    justify-content: flex-start;
    .g-legend-item {
      margin-left: 0.17rem;
      &.blue {
        width: 40%
      }
    }
  }
</style>
